package mysql

import (
	"gorm.io/gorm"
	"short-vid-joy/shortrpc/shortrpc"
)

type Collect struct {
	gorm.Model
	UserId        int64 `gorm:"type:int(11);not null;comment:'用户id'"`
	VideoId       int64 `gorm:"type:int(11);not null;comment:'视频id'"`
	CollectStatus int64 `gorm:"type:int(11);default:0;not null;comment:'收藏状态,0-已收藏，1-已取消'"`
}

func (c *Collect) CollectAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collect) FindCollectById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).First(c).Error
}

func (c *Collect) DelCollect(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Delete(c).Error
}

func CollectList(db *gorm.DB, in *shortrpc.CollectListRequest, list []*shortrpc.CollectList) []*shortrpc.CollectList {
	db.Debug().Model(&Collect{}).
		Select("collects.id as collect_id,video_image,video_title,head_image,nick_name,play_num").
		Joins("left join videos on collects.video_id=videos.id").
		Joins("left join users on collects.user_id=users.id").
		Where("collects.user_id = ?", in.UserId).
		Scopes(Paginate(int(in.Page), int(in.PageSize))).Find(&list)
	return list
}
